// EDataTable.qml
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Effects
import "qrc:/component/ZGlobal.js" as Global

Item {
    id: root
    clip: false
    height:contentColumn.height
    // === 接口属性 & 信号 ===
    property var headers:  [
        { key: "id", label: "序号", w: 60},
        { key: "temperature", label: "温度", w: 250 },
        { key: "speed", label: "速度", w: 250 },
        { key: "weight", label: "重量", w: 250 },
        { key: "ph", label: "pH", w:250 },
        { key: "conductivity", label: "电导", w:250 },
        { key: "create_time", label: "采集时间",w: 0},
    ]
    property var model: batchdataInfo
    property bool selectable: false
    signal rowClicked(int index, var rowData)
    signal checkStateChanged(int index, var rowData, bool isChecked)
    signal editClicked(int index, var rowData)
    signal addClicked()

    // === 样式属性 ===
    property bool backgroundVisible: true
    property real radius: 12
    property int headerHeight: 60
    property int rowHeight: 45
    property int fontSize: 24
    property int cellPadding:62
    property real pressedScale: 0.97
    property color headerColor: theme.secondaryColor
    property color rowColor: "#FFFFFF"
    property color hoverColor: Qt.darker(rowColor, 1.0)
    property color textColor: "#333"
    property color headerTextColor: theme.textColor
    property bool shadowEnabled: true
    property color shadowColor: theme.shadowColor
    property color checkmarkColor: theme.focusColor
    property int boxSize:10

    property color gridLineColor: "#DDE7F5"

    // === 动态列宽数组 ===
    property var columnWidths: []
    // 外部可注入的头部工具区内容
    //property alias headerTools: headerToolsContainer.data

    TextMetrics {
        id: textMetrics
        font.pixelSize: root.fontSize
    }

    // === 响应式表头全选状态 ===
    property string headerCheckState: "none" // none / all / partial

    onRowClicked: function(index, rowData) {
        console.log("点击行：", index, rowData.name)
    }

    onEditClicked: function(index, rowData) {
        console.log("编辑行：", index, rowData.id, ">>", swipeView.contentChildren[1].name)
        var batch = batchInfo.get(index);
        console.log("name:", batch.name, " real_name", batch.real_name," id:", batch.id)
        batchPage.fillData(index, batch)
        swipeView.currentIndex = 1;
    }
    // 计算内容总宽度（考虑特殊列的最小宽度）
    function effectiveColumnWidth(index) {
        return headers[index].w;
    }

    function computeContentWidth() {
        var sum = root.selectable ? 40 : 0;
        if (!headers || headers.length === 0) return sum;
        for (var i = 0; i < headers.length; i++) {
            sum += effectiveColumnWidth(i);
        }
        return sum;
    }

    // === 背景与阴影===

    Column {
        id: contentColumn
        width: parent.width
        // === 表头 ===
        Row {
            id: headerRow
            height: root.headerHeight
            width: parent.width
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 0
            // 数据列头
            Repeater {
                model: root.headers
                delegate: Rectangle {
                    width: effectiveColumnWidth(index) ===0 ? headerRow.width-computeContentWidth():effectiveColumnWidth(index)
                    height: parent.height
                    color: root.backgroundVisible ? "#208FCD" : "transparent"
                    border.width: 1
                    border.color: gridLineColor

                    Text {
                        anchors.centerIn: parent
                        text: modelData.label
                        font.pixelSize: root.fontSize
                        font.bold: true
                        color: root.headerTextColor
                        elide: Text.ElideRight
                    }
                }
            }
        }

        // === 数据区 ===
        ListView {
            id: tableView
            width: parent.width
            height: contentHeight
            spacing: 0
            anchors.horizontalCenter: parent.horizontalCenter
            model: root.model
            clip: true

            delegate: Rectangle {
                id: rowContainer
                width: headerRow.width
                height: root.rowHeight
                radius: root.radius * 0.5
                property bool checked: (model.checked !== undefined) ? model.checked : false
                property var rowData: model
                property int rowIndex: index
                property bool hovered: false

                color: "transparent"
                opacity: 1.0

                Behavior on color { ColorAnimation { duration: 150 } }
                Behavior on opacity { NumberAnimation { duration: 100 } }

                transform: Scale {
                    id: scale
                    origin.x: rowContainer.width / 2
                    origin.y: rowContainer.height / 2
                }

                ParallelAnimation {
                    id: restoreAnimation
                    SpringAnimation { target: scale; property: "xScale"; to: 1.0; spring: 2.5; damping: 0.25 }
                    SpringAnimation { target: scale; property: "yScale"; to: 1.0; spring: 2.5; damping: 0.25 }
                }

                Row {
                    anchors.fill: parent
                    anchors.horizontalCenter: parent.horizontalCenter
                    spacing: 0

                    // 数据列
                    Repeater {
                        model: root.headers
                        delegate: Rectangle {
                            property bool isActionCol: modelData.key === "actions"
                            property bool isStatusCol: modelData.key === "status"
                            property bool isPermissionCol: modelData.key === "permission"
                            width: effectiveColumnWidth(index) ===0 ? headerRow.width-computeContentWidth():effectiveColumnWidth(index)
                            height: parent.height
                            color: "transparent"
                            border.width: 1
                            border.color: gridLineColor

                            // 普通数据列
                            Text {
                                visible: !isActionCol && !isStatusCol && !isPermissionCol
                                anchors.fill: parent
                                text: displayValue(modelData.key, rowData)
                                verticalAlignment: Text.AlignVCenter
                                horizontalAlignment: Text.AlignHCenter
                                color: root.textColor
                                font.pixelSize: root.fontSize
                                elide: Text.ElideRight
                                wrapMode: Text.WordWrap
                            }
                            Text {
                                visible: isActionCol
                                anchors.centerIn: parent
                                text: "详情"
                                color: "#2090CD"
                                font.pixelSize: root.fontSize
                            }
                            MouseArea {
                                visible: isActionCol
                                anchors.fill: parent
                                cursorShape: Qt.PointingHandCursor
                                onClicked: root.editClicked(rowContainer.rowIndex, rowContainer.rowData)
                            }
                        }
                    }
                }
            }
        }

    }
    // 显示值格式化（时间列显示完整时间）
    function displayValue(key, rowObj) {
        var v = rowObj[key];
        if (v === undefined) return "-";
        var s = String(v);
        // 时间列显示完整时间格式，不再截取
        return s;
    }
}
